package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.work.keep.R;
import com.work.keep.application.MyApplication;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.SingleWorkoutData;

import java.io.IOException;
import java.util.List;


/**
 * Created by dllo on 16/1/23.
 */
public class VideoActivity extends BaseActivity implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener {
    private SurfaceView videoActivitySurface;
    private RatingBar myRavideoActivityRatingBar;
    private TextView surfacePoint;
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
    private static List<SingleWorkoutData.DataEntity.StepsEntity> stepEntity;
    private static int position;
    private String url;

    public static void goToVideoActivity(Context context, List<SingleWorkoutData.DataEntity.StepsEntity> stepEntity, int position) {
        Intent intent = new Intent(context, VideoActivity.class);
        VideoActivity.stepEntity = stepEntity;
        VideoActivity.position = position;
        context.startActivity(intent);
    }

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_video;
    }

    @Override
    protected void initView() {
        videoActivitySurface = (SurfaceView) findViewById(R.id.videoActivitySurface);
        myRavideoActivityRatingBar = (RatingBar) findViewById(R.id.videoActivityRatingBar);
        surfacePoint = (TextView) findViewById(R.id.surfacePoint);


    }

    @Override
    protected void initData() {

        url = stepEntity.get(position).getExercise().getVideos().get(1).getUrl();
        myRavideoActivityRatingBar.setMax(3);
        myRavideoActivityRatingBar.setRating(1);
        surfacePoint.setText(stepEntity.get(position).getExercise().getDescription());
        surfaceHolder = videoActivitySurface.getHolder();
        surfaceHolder.addCallback(this);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mediaPlayer.setDataSource(this, Uri.parse(url));
            mediaPlayer.setDisplay((surfaceHolder));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    protected void onDestroy() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
        super.onDestroy();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mediaPlayer.start();
    }
}
